package com.yang.flower.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yang.flower.common.Result;
import com.yang.flower.config.AuthAccess;
import com.yang.flower.entity.Flower;
import com.yang.flower.entity.User;
import com.yang.flower.mapper.FileMapper;
import com.yang.flower.service.FlowerService;
import com.yang.flower.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: TODO
 * @Author: Yang
 * @DateTime: 2022/12/8 20:54
 **/
@RestController
@CrossOrigin
@RequestMapping("/echarts")
public class EchartsController {
    
    @Autowired
    private UserService userService;

    @Resource
    private FileMapper fileMapper;

    @Autowired
    private FlowerService flowerService;

    @AuthAccess
    @GetMapping("/example")
    public Result get(){
        Map<String,Object> map = new HashMap<>();
        map.put("x", CollUtil.newArrayList("Mon","Tue","Thu","Fri","Sat","Sun"));
        map.put("y",CollUtil.newArrayList(150,230,224,218,135,147,260));
        return Result.success(map);
    }

    @AuthAccess
    @GetMapping("/members")
    public Result members(){
        List<User> list = userService.list();
        int q1 = 0;
        int q2 = 0;
        int q3 = 0;
        int q4 = 0;
        for (User user : list) {
            Date createTime = user.getCreateTime();
            Quarter quarter = DateUtil.quarterEnum(createTime);
            switch(quarter){
                case Q1: q1 += 1; break;
                case Q2: q2 += 1; break;
                case Q3: q3 += 1; break;
                case Q4: q4 += 1; break;
            }
        }
        return Result.success(CollUtil.newArrayList(q1,q2,q3,q4));
    }

    @AuthAccess
    @GetMapping("/flowerTop")
    public Result flowerTop(){
        LambdaQueryWrapper<Flower> queryWrapper = new LambdaQueryWrapper<>();
        List<Flower> flowerList = flowerService.list(queryWrapper.orderByDesc(Flower::getSales));
        List<Flower> flowers = flowerList.subList(0, 5);
        return Result.success(flowers);
    }

//    @AuthAccess
//    @GetMapping("/file/front/all")
//    public Result frontAll(){
//        return Result.success(fileMapper.selectList(null));
//    }

}
